#!/usr/bin/python
# -*- coding:utf-8 -*-
# @FileName : python_test10_13.py
# Author    : myh

# 练习 10.13：用户字典　示例 remember_me.py 只存储了一项信息——
# 用户名。请扩展该示例，让用户同时提供另外两项信息，再将收集到
# 的所有信息存储到一个字典中。使用 json.dumps() 将这个字典写入
# 文件，并使用 json.loads() 从文件中读取它。打印一条摘要消息，
# 指出程序记住了有关用户的哪些信息。
from pathlib import Path
import json

username = input("What is your name? ")
age = input("What is your age? ")
high = input("What is your high? ")
# 文件路径
path = Path('username1.json')
try:
    contents = path.read_text(encoding='gbk')
except FileNotFoundError:
    print('出错了')
else:
    if len(contents) == 0:
        contents = []
    else:
        contents = json.loads(contents)
    print(contents)
    if username in contents:
        print('您以及注册过了')
    else:
        new_people_info={}
        new_people_info['username'] = username
        new_people_info['age'] = age
        new_people_info['high'] = high

        contents.append(new_people_info)
        contents = json.dumps(contents)
        path.write_text(contents)
        print(f"We'll remember you when you come back, {contents}!")
